/**
 * @Author: Gao Chenxi
 * @Description:
 * @Date: 2020/4/1 4:35 PM
 * @File: main
 */
package main

import (
	"fmt"
	"github.com/BSNDA/PCNGateway-Go-SDK/pkg/client/fabric"
	"github.com/BSNDA/PCNGateway-Go-SDK/pkg/core/config"
	"github.com/BSNDA/PCNGateway-Go-SDK/router"
	sy "github.com/BSNDA/PCNGateway-Go-SDK/sysconf"
	"github.com/gin-gonic/gin"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	//"time"
	//"github.com/BSNDA/PCNGateway-Go-SDK/pkg/core/entity/req/fabric/user"
	"log"
)
type BCMessage struct {
	Body string
}
var cfg = pflag.StringP("config", "c", "", "apiserver config file path.")

func main() {


	// * 初始化配置
	pflag.Parse()
	if err := sy.Init(*cfg); err != nil {
		panic(err)
	}
	log.Printf("The sysconfig inited.")


	// * 设置 run mode
	gin.SetMode(viper.GetString("runmode"))
	log.Printf("Gin run mode set to: %s", viper.GetString("runmode"))

	// * gin engine.
	g := gin.New()

	router.Load(g)
	log.Printf("The gin engine started, and the router loaded.")
	log.Printf("Start to listening on: %s", viper.GetString("addr"))
	g.Run(viper.GetString("addr")).Error()


	fmt.Println("github.com/BSNDA/PCNGateway-Go-SDK")
	api := viper.GetString("bc.api")     //PCN gateway address
	userCode := viper.GetString("bc.userCode") //user code
	appCode := viper.GetString("bc.appCode")  //DApp code
	puk := "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEV5+GtbQNzwfPsMfVIFNBg3xgRuwV\n2RDS+Rjd72F6qXGwrb8QLSDUitNinENWuowAEsu/U9GsfSn0A48xlRTfSg==\n-----END PUBLIC KEY-----"      //public key
	prk := "-----BEGIN PRIVATE KEY-----\nMIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgVbS8svrC3sdFgIFW\nUoLfczfjCjq0z7fGTSFfqMkjssChRANCAARXn4a1tA3PB8+wx9UgU0GDfGBG7BXZ\nENL5GN3vYXqpcbCtvxAtINSK02KcQ1a6jAASy79T0ax9KfQDjzGVFN9K\n-----END PRIVATE KEY-----"      //private key
	mspDir := ""   //cert storage directory
	cert := ""     //cert

	config, err := config.NewConfig(api, userCode, appCode, puk, prk, mspDir, cert)
	if err != nil {
		log.Fatal(err)
	}
	fabric.InitFabricClient(config)

}
